package com.ns002.yigou.quote.json;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ns002.yigou.quote.exception.ApplicationException;
import com.ns002.yigou.quote.exception.LogicException;
import com.ns002.yigou.quote.globals.ResultStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;

import java.util.Collections;

/**
 * Created by xu on 2016/12/14.
 * 用于封装返回客户端的JSON数据
 */
@ApiModel
public class JSONResult<T> {

    static Logger logger = LoggerFactory.getLogger(JSONResult.class);

    private int code = 200;
    private String msg;
    private T data;

    public static final JSONResult createErrorResult(int code, String msg){
        JSONResult result = new JSONResult();
        result.code = code;
        result.msg = msg;
        return result;
    }

    public static final JSONResult createErrorResult(ResultStatus resultStatus){
        JSONResult result = new JSONResult();
        result.code = resultStatus.getStatusCode();
        result.msg = resultStatus.getStatusMessage();
        return result;
    }

    public static final JSONResult createErrorResult(LogicException e){
        JSONResult result = new JSONResult();
        result.code = e.getCode();
        result.msg =  e.getMsg();
        return result;
    }

    public static final JSONResult createErrorResult(ApplicationException e){
        return createErrorResult(e.getResultStatus());
    }

    public static final JSONResult createErrorResult(Throwable e){
        if(e instanceof  LogicException){
            return createErrorResult((LogicException) e);
        } else if(e instanceof  ApplicationException) {
            return createErrorResult((ApplicationException) e);
        }

        logger.error("数据返回出现错误", e);
        return createErrorResult(new LogicException(e.toString()));
    }

    public static final JSONResult createErrorResult(){
        JSONResult result = new JSONResult();
        result.code = ResultStatus.ERROR.getStatusCode();
        result.msg = ResultStatus.ERROR.getStatusMessage();
        return result;
    }

    public static final JSONResult createSuccessResult(Object data){
        JSONResult result = new JSONResult();
        result.code = ResultStatus.SUCCESS.getStatusCode();
        result.msg = ResultStatus.SUCCESS.getStatusMessage();
        result.data = data;
        return result;
    }

    public static final JSONResult createSuccessResult(){
        return createSuccessResult(null);
    }

    @ApiModelProperty(name = "data", value = "数据",required = true)
    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        String json = JSONObject.toJSONString(this, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty
                , SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullBooleanAsFalse);

        return json;
    }

    /**
     * 处理spring验证数据
     * @param bindingResult
     */
    public static void handleBindingResult(BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new LogicException(bindingResult.getFieldError().getDefaultMessage());
        }
    }

    @ApiModelProperty(name = "code", value = "状态码",required = true, example = "200")
    public int getCode() {
        return code;
    }

    @ApiModelProperty(name = "msg", value = "描述",required = true, example = "请求成功")
    public String getMsg() {
        return msg;
    }
}
